#include "head.h"

char currentLoginUserName[128] = {0};  // 登录用户名称
char currentLoginFormName[128] = {0};  // 登录用户表名称
char currentLoginHouseName[128] = {0}; // 登录用户仓库名称

int DATA = 1;
int DATA_COL = 1;

sqlite3 *pDB = NULL; // 数据库的指针

LOGIN_INFO loginInfoArr[1000]; // 登录信息数组
int loginCount = 0;            // 登录的人数，有时候也作为下标用

// 工作线程，接受服务器返回来的消息
void *work(void *pArg)
{
    int sID = *(int *)pArg;
    // 用户连接后会创建当前线程，并且会把用户的套接字id通过参数传入进来
    // 这里是把套接字从地址里面取出来
    char buf[128]; // 这个数组的大小要和客户端发数据的大小一致
    while (0 < read(sID, buf, sizeof(buf)))
    {
        printf("接受到服务器发来的消息%s\n", buf);
    }
    printf("接受数据出错了，可能是客户端被关掉了\n");
}

// 仓库功能函数
void warehouse(int sID, MSG msg, struct sockaddr_in clientAddr)
{
    if (msg.type == regist)
    {
        // 把缓冲区的类型转为注册
        RGU *pRgu = (RGU *)msg.buf;
        char szSql[512];
        // 把用户名和密码插入到数据表中
        sprintf(szSql, "INSERT INTO abc (name, pwd)\
               VALUES ( '%s', '%s' )",
                pRgu->name, pRgu->pwd);

        if (-1 == execSql(pDB, szSql))
        {
            printf("注册用户失败\n");
        }
        else
        {
            msg.type = ok;
            int ret = send(sID, &msg, sizeof(msg), 0);
            if (ret == -1)
            {
                printf("发送数据出错了\n");
            }
        }
    }
    else if (msg.type == creative)
    {
        RGU *pRgu = (RGU *)msg.buf;
        char szSql[512];
        sprintf(szSql, "UPDATE abc SET warehouse_name = '%s' WHERE name = '%s';",
                pRgu->name_warehouse, pRgu->name);
        if (-1 == execSql(pDB, szSql))
        {
            printf("仓库创建失败失败\n");
        }
        else
        {
            msg.type = ok;
            int ret = send(sID, &msg, sizeof(msg), 0);
            if (ret == -1)
            {
                printf("发送数据出错了\n");
            }
        }
    }
    else if (msg.type == login)
    {
        // 把缓冲区的类型转为登录，因为注册和登录用的是同一个结构体
        RGU *pRgu = (RGU *)msg.buf;

        int ret = 0;
        RGU rgu;
        // 因为登录成功了，所以把消息类型改为ok，再发回去
        msg.type = no;

        char sql[512];
        sprintf(sql, "select * from abc where name = '%s' AND pwd = '%s';", pRgu->name, pRgu->pwd);
        QUREY_RST qr;
        // 执行查询语句
        querySql(pDB, sql, &qr);
        // 如果结果的行数是>0的，说明数据库中有记录，那么登陆成功
        if (qr.row > 0)
        {
            // 因为登录成功了，所以把消息类型改为ok，再发回去
            msg.type = ok;
            // 把登录成功的用户名和地址保存起来
            strcpy(loginInfoArr[loginCount].name, rgu.name);
            loginInfoArr[loginCount].fd = sID; // 登录成功后记住用户的套接字
            loginCount++;
        }

        printf("\t  用户登录成功\n");
        // 把消息发回客户端，告诉它登录成功了
        ret = send(sID, &msg, sizeof(msg), 0);
        if (ret == -1)
        {
            printf("发送数据出错了\n");
        }
    }
    else if (msg.type == check)
    {
        CHECK_DATA check_data;
        char buf[128];
        sprintf(buf, "select * from abc where name = '%s';", msg.buf);
        QUREY_RST qr;
        // 执行查询语句
        pDB = openSqlite3("abc");
        querySql(pDB, buf, &qr);
        strcpy(check_data.name_data, qr.pzResult[qr.col * qr.row + 3]);
        check_data.col = qr.col;
        check_data.row = qr.row;
        memcpy(msg.buf, &check_data, sizeof(check_data));
        msg.type = finish;
        int ret = send(sID, &msg, sizeof(msg), 0);
        if (ret == -1)
        {
            printf("发送数据出错了\n");
            return;
        }
    }
    else if (msg.type == updata)
    {
        UP_DATA *pRgu = (UP_DATA *)msg.buf;
        char buf[128];
        pDB = openSqlite3("abc");
        sprintf(buf, "select * from abc where name = '%s';", pRgu->user_name);
        // 将用户上传的数据表明写入用户数据表中
        QUREY_RST qr;
        // 执行查询语句
        querySql(pDB, buf, &qr);
        sprintf(buf, "UPDATE abc SET name_form = '%s';", pRgu->form_name);
        execSql(pDB, buf);
        sprintf(buf, "DROP TABLE IF EXISTS '%s' ", pRgu->form_name);
        execSql(pDB, buf);
        // 创建表用来存放用户上传的数据
        sprintf(buf, "CREATE TABLE IF NOT EXISTS '%s' (\
                    id INTEGER PRIMARY KEY AUTOINCREMENT, \
                    name VARCHAR(50) , \
                    phone_number VARCHAR(50) , \
                    ID_number VARCHAR(50) )",
                pRgu->form_name);
        execSql(pDB, buf);
    }
    else if (msg.type == send_name)
    {
        char buf[128];
        pDB = openSqlite3("abc");
        UP_DATA *pSC = (UP_DATA *)msg.buf;
        sprintf(buf, "INSERT INTO '%s' (name) \
                        VALUES ('%s');",
                pSC->form_name, pSC->form_data);
        execSql(pDB, buf);
    }
    else if (msg.type == send_phone)
    {
        char buf[128];
        pDB = openSqlite3("abc");
        UP_DATA *pSC = (UP_DATA *)msg.buf;
        sprintf(buf, "UPDATE '%s' SET phone_number = '%s' \
                        WHERE id = '%d';",
                pSC->form_name, pSC->form_data, DATA_COL);
        execSql(pDB, buf);
        DATA_COL++;
    }
    else if (msg.type == send_ID)
    {
        char buf[128];
        pDB = openSqlite3("abc");
        UP_DATA *pSC = (UP_DATA *)msg.buf;
        sprintf(buf, "UPDATE '%s' SET ID_number = '%s' \
                        WHERE id = '%d';",
                pSC->form_name, pSC->form_data, DATA);
        execSql(pDB, buf);
        DATA++;
    }
    else if (msg.type == send_over)
    {
        DATA = 1;
        DATA_COL = 1;
    }
    else if (msg.type == Recover)
    {
        UP_DATA *pRgu = (UP_DATA *)msg.buf;
        QUREY_RST *p = NULL;
        MSG msg;
        strcpy(currentLoginUserName, pRgu->user_name);
        strcpy(currentLoginFormName, pRgu->form_name);
        strcpy(currentLoginHouseName, pRgu->form_data);
        p = check_house(sID);
        for (int i = 1; i <= p->row; i++)
        {
            msg.type = resend_name;
            memcpy(msg.buf, p->pzResult[i * (p->col) + 1], sizeof(MSG));
            int ret = send(sID, &msg, sizeof(msg), 0);
            if (ret == -1)
            {
                printf("发送数据出错了\n");
                return;
            }
            sleep(1);
        }
        for (int i = 1; i <= p->row; i++)
        {
            msg.type = resend_phone;
            memcpy(msg.buf, p->pzResult[i * (p->col) + 2], sizeof(MSG));
            int ret = send(sID, &msg, sizeof(msg), 0);
            if (ret == -1)
            {
                printf("发送数据出错了\n");
                return;
            }
            sleep(1);
        }

        for (int i = 1; i <= p->row; i++)
        {
            msg.type = resend_ID;
            memcpy(msg.buf, p->pzResult[i * (p->col) + 3], sizeof(MSG));
            int ret = send(sID, &msg, sizeof(msg), 0);
            if (ret == -1)
            {
                printf("发送数据出错了\n");
                return;
            }
            sleep(1);
        }
        msg.type = resend_over;
        int ret = send(sID, &msg, sizeof(msg), 0);
        if (ret == -1)
        {
            printf("发送数据出错了\n");
            return;
        }
    }
    
}

// 注册函数
int user_regist(int sID)
{
    RGU rgu;
    printf("请输入用户名和密码\n");
    scanf("%s%s", rgu.name, rgu.pwd);

    MSG msg;
    msg.type = regist;
    memcpy(msg.buf, &rgu, sizeof(rgu)); // 把注册结构体拷贝到消息结构体的仓库中

    // 把消息发送给服务器
    int ret = send(sID, &msg, sizeof(msg), 0);
    if (ret == -1)
    {
        printf("发送数据出错了\n");
        return -1;
    }
    // 接受服务器的回复
    struct sockaddr_in serverAddr;
    socklen_t len = sizeof(serverAddr);
    // clientAddr是对方的地址,len地址的长度
    ret = recv(sID, &msg, sizeof(msg), 0);
    if (msg.type == ok)
    {
        printf("注册成功，正在为您创建仓库·············\n");
        sleep(5);
        creat_warehouse(sID, msg, rgu);
    }
}

// 创建仓库函数
int creat_warehouse(int sID, MSG msg, RGU rgu)
{
    msg.type = creative;
    char name_warehouse;
    printf("请输入您的仓库名称：");
    scanf("%s", &name_warehouse);
    pDB = openSqlite3(&name_warehouse);
    char szSQL[256];
    sprintf(szSQL, "CREATE TABLE '%s' (\
           id INTEGER PRIMARY KEY AUTOINCREMENT,\
           name_form VARCHAR(50));",
            &name_warehouse);

    if (-1 == execSql(pDB, szSQL))
    {
        printf("创建用户表失败\n");
        return -1;
    }
    strcpy(rgu.name_warehouse, &name_warehouse);
    if (pDB == NULL)
    {
        printf("打开或者创建数据库失败\n");
        return -1;
    }
    memcpy(msg.buf, &rgu, sizeof(rgu));
    int ret = send(sID, &msg, sizeof(msg), 0);
    if (ret == -1)
    {
        printf("发送数据出错了\n");
        return -1;
    }
    // 接受服务器的回复
    struct sockaddr_in serverAddr;
    socklen_t len = sizeof(serverAddr);
    // clientAddr是对方的地址,len地址的长度
    ret = recv(sID, &msg, sizeof(msg), 0);
    if (msg.type == ok)
    {
        printf("创建仓库成功\n");
    }
    return 1;
}

// 登录函数
int user_login(int sID)
{
    RGU rgu; // 登录可以重用注册的结构体
    printf("请输入用户名和密码\n");
    scanf("%s%s", rgu.name, rgu.pwd);

    MSG msg;
    msg.type = login;
    memcpy(msg.buf, &rgu, sizeof(rgu)); // 把注册结构体拷贝到消息结构体的仓库中

    // 把消息发送给服务器
    int ret = send(sID, &msg, sizeof(msg), 0);
    if (ret == -1)
    {
        printf("发送数据出错了\n");
        return -1;
    }

    // 接受服务器的回复
    struct sockaddr_in serverAddr;
    socklen_t len = sizeof(serverAddr);
    // clientAddr是对方的地址,len地址的长度
    ret = recv(sID, &msg, sizeof(msg), 0);
    if (msg.type == ok)
    {
        printf("登陆成功\n");
        strcpy(currentLoginUserName, rgu.name); // 把用户名保存到全局变量中，其他地方都可以使用
        return 1;
    }
    else
    {
        printf("还没有注册或者用户名密码错误\n");
        return -1;
    }
}

// 用户菜单函数
int user_menu(int sID)
{
    QUREY_RST *qr;
    sleep(1);
    printf("\033[43m请输入对应的序号进行操作\033[0m\n");
    printf("\t  1：查看个人仓库\n\t  2：上传数据库备份\n\t  3：恢复本地数据库\n\t  4：分享个人仓库\n\t  5：下载云仓库数据\n\t  6：退出登录\n");
    int expression;
    scanf("%d", &expression);
    switch (expression)
    {
    case 1:
        system("clear");
        qr = check_warehouse(sID);
        data_print(qr); // 打印函数
        break;

    case 2:
        system("clear");
        printf("正在为您备份数据···············\n");
        check_house(sID);
        upload_data(sID);
        printf("备份数据成功\n");
        break;

    case 3:
        system("clear");
        printf("正在为您恢复数据···············\n");
        recover_warehouse(sID);
        printf("恢复数据成功\n");
        break;

    case 4:
        system("clear");
        printf("正在为您上传数据至云仓库···············\n");
        check_house(sID);
        share_warehousea(sID);
        printf("分享成功\n");
        break;

    case 5:
        system("clear");
        printf("正在为您从云仓库下载数据···············\n");
        download_warehousea(sID);
        printf("下载成功\n");
        break;

    default:
        
        return -1;
    }
}

// 下载云数据库函数
int download_warehousea(int sID)
{
    UP_DATA up_data;
    MSG msg;
    QUREY_RST qr;
    char buf[128];
    check_house(sID);
    msg.type = Recover;
    strcpy(currentLoginFormName, "public_data");
    strcpy(up_data.user_name, currentLoginUserName);
    strcpy(up_data.form_name, currentLoginFormName);
    strcpy(up_data.form_data, currentLoginHouseName);
    memcpy(msg.buf, &up_data, sizeof(UP_DATA));
    int ret = send(sID, &msg, sizeof(MSG), 0);
    printf("ret = %d\n", ret);
    if (ret == -1)
    {
        printf("发送数据出错了\n");
        return -1;
    }
    pDB = openSqlite3(currentLoginHouseName);
    sprintf(buf, "DROP TABLE IF EXISTS '%s' ", currentLoginFormName);
    execSql(pDB, buf);
    sprintf(buf, "CREATE TABLE IF NOT EXISTS '%s' (\
                    id INTEGER PRIMARY KEY AUTOINCREMENT, \
                    name VARCHAR(50) , \
                    phone_number VARCHAR(50) , \
                    ID_number VARCHAR(50) )",
            currentLoginFormName);
    execSql(pDB, buf);
    struct sockaddr_in serverAddr;
    socklen_t len = sizeof(serverAddr);
    while (0 < (ret = recv(sID, &msg, sizeof(msg), 0)))
    {
        if (msg.type == resend_name)
        {
            char buf[128];
            sprintf(buf, "INSERT INTO '%s' (name) \
                        VALUES ('%s');",
                    currentLoginFormName, msg.buf);
            execSql(pDB, buf);
        }
        else if (msg.type == resend_phone)
        {
            char buf[128];
            sprintf(buf, "UPDATE '%s' SET phone_number = '%s' \
                        WHERE id = '%d';",
                    currentLoginFormName, msg.buf, DATA_COL);
            DATA_COL++;
            execSql(pDB, buf);
        }
        else if (msg.type == resend_ID)
        {
            char buf[128];
            sprintf(buf, "UPDATE '%s' SET ID_number = '%s' \
                        WHERE id = '%d';",
                    currentLoginFormName, msg.buf, DATA);
            DATA++;
            execSql(pDB, buf);
        }
        else if (msg.type == resend_over)
        {
            DATA = 1;
            DATA_COL = 0;
            break;
        }
    }
}

// 分享数据库函数
int share_warehousea(int sID)
{
    int ret;
    char buf[128];
    MSG msg;
    UP_DATA up_data;
    msg.type = updata;
    QUREY_RST *p = NULL;
    struct sockaddr_in serverAddr;
    socklen_t len = sizeof(serverAddr);
    p = check_warehouse(sID);
    strcpy(up_data.user_name, currentLoginUserName);
    strcpy(up_data.form_name, "public_data");
    memcpy(msg.buf, &up_data, sizeof(UP_DATA));
    ret = send(sID, &msg, sizeof(msg), 0);
    if (ret == -1)
    {
        printf("发送数据出错了\n");
        return -1;
    }
    sleep(1);
    for (int i = 1; i <= p->row; i++)
    {
        msg.type = send_name;
        memcpy(up_data.form_data, p->pzResult[i * (p->col) + 1], sizeof(QUREY_RST));
        memcpy(msg.buf, &up_data, sizeof(UP_DATA));
        ret = send(sID, &msg, sizeof(msg), 0);
        if (ret == -1)
        {
            printf("发送数据出错了\n");
            return -1;
        }
        sleep(1);
    }

    for (int i = 1; i <= p->row; i++)
    {
        msg.type = send_phone;
        memcpy(up_data.form_data, p->pzResult[i * (p->col) + 2], sizeof(QUREY_RST));
        memcpy(msg.buf, &up_data, sizeof(UP_DATA));
        ret = send(sID, &msg, sizeof(msg), 0);
        if (ret == -1)
        {
            printf("发送数据出错了\n");
            return -1;
        }
        sleep(1);
    }

    for (int i = 1; i <= p->row; i++)
    {
        msg.type = send_ID;
        memcpy(up_data.form_data, p->pzResult[i * (p->col) + 3], sizeof(QUREY_RST));
        memcpy(msg.buf, &up_data, sizeof(UP_DATA));
        ret = send(sID, &msg, sizeof(msg), 0);
        if (ret == -1)
        {
            printf("发送数据出错了\n");
            return -1;
        }
        sleep(1);
    }

    msg.type = send_over;
    ret = send(sID, &msg, sizeof(msg), 0);
    if (ret == -1)
    {
        printf("发送数据出错了\n");
        return -1;
    }
}

// 恢复本地数据函数
int recover_warehouse(int sID)
{
    UP_DATA up_data;
    MSG msg;
    QUREY_RST qr;
    char buf[128];
    check_house(sID);
    msg.type = Recover;
    strcpy(up_data.user_name, currentLoginUserName);
    strcpy(up_data.form_name, currentLoginFormName);
    strcpy(up_data.form_data, currentLoginHouseName);
    memcpy(msg.buf, &up_data, sizeof(UP_DATA));
    int ret = send(sID, &msg, sizeof(MSG), 0);
    if (ret == -1)
    {
        printf("发送数据出错了\n");
        return -1;
    }
    pDB = openSqlite3(currentLoginHouseName);
    sprintf(buf, "DROP TABLE IF EXISTS '%s' ", currentLoginFormName);
    execSql(pDB, buf);
    sprintf(buf, "CREATE TABLE IF NOT EXISTS '%s' (\
                    id INTEGER PRIMARY KEY AUTOINCREMENT, \
                    name VARCHAR(50) , \
                    phone_number VARCHAR(50) , \
                    ID_number VARCHAR(50) )",
            currentLoginFormName);
    execSql(pDB, buf);
    struct sockaddr_in serverAddr;
    socklen_t len = sizeof(serverAddr);
    while (0 < (ret = recv(sID, &msg, sizeof(msg), 0)))
    {
        if (msg.type == resend_name)
        {
            char buf[128];
            sprintf(buf, "INSERT INTO '%s' (name) \
                        VALUES ('%s');",
                    currentLoginFormName, msg.buf);
            execSql(pDB, buf);
        }
        else if (msg.type == resend_phone)
        {
            char buf[128];
            sprintf(buf, "UPDATE '%s' SET phone_number = '%s' \
                        WHERE id = '%d';",
                    currentLoginFormName, msg.buf, DATA_COL);
            DATA_COL++;
            execSql(pDB, buf);
        }
        else if (msg.type == resend_ID)
        {
            char buf[128];
            sprintf(buf, "UPDATE '%s' SET ID_number = '%s' \
                        WHERE id = '%d';",
                    currentLoginFormName, msg.buf, DATA);
            DATA++;
            execSql(pDB, buf);
        }
        else if (msg.type == resend_over)
        {
            DATA = 1;
            DATA_COL = 0;
            break;
        }
    }
}

// 上传数据函数
int upload_data(int sID)
{
    int ret;
    char buf[128];
    MSG msg;
    UP_DATA up_data;
    msg.type = updata;
    QUREY_RST *p = NULL;
    struct sockaddr_in serverAddr;
    socklen_t len = sizeof(serverAddr);
    p = check_warehouse(sID);
    strcpy(up_data.user_name, currentLoginUserName);
    strcpy(up_data.form_name, currentLoginFormName);
    memcpy(msg.buf, &up_data, sizeof(UP_DATA));
    ret = send(sID, &msg, sizeof(msg), 0);
    if (ret == -1)
    {
        printf("发送数据出错了\n");
        return -1;
    }
    sleep(1);
    for (int i = 1; i <= p->row; i++)
    {
        msg.type = send_name;
        memcpy(up_data.form_data, p->pzResult[i * (p->col) + 1], sizeof(QUREY_RST));
        memcpy(msg.buf, &up_data, sizeof(UP_DATA));
        ret = send(sID, &msg, sizeof(msg), 0);
        if (ret == -1)
        {
            printf("发送数据出错了\n");
            return -1;
        }
        sleep(1);
    }

    for (int i = 1; i <= p->row; i++)
    {
        msg.type = send_phone;
        memcpy(up_data.form_data, p->pzResult[i * (p->col) + 2], sizeof(QUREY_RST));
        memcpy(msg.buf, &up_data, sizeof(UP_DATA));
        ret = send(sID, &msg, sizeof(msg), 0);
        if (ret == -1)
        {
            printf("发送数据出错了\n");
            return -1;
        }
        sleep(1);
    }

    for (int i = 1; i <= p->row; i++)
    {
        msg.type = send_ID;
        memcpy(up_data.form_data, p->pzResult[i * (p->col) + 3], sizeof(QUREY_RST));
        memcpy(msg.buf, &up_data, sizeof(UP_DATA));
        ret = send(sID, &msg, sizeof(msg), 0);
        if (ret == -1)
        {
            printf("发送数据出错了\n");
            return -1;
        }
        sleep(1);
    }

    msg.type = send_over;
    ret = send(sID, &msg, sizeof(msg), 0);
    if (ret == -1)
    {
        printf("发送数据出错了\n");
        return -1;
    }
}

// 查看个人仓库函数
QUREY_RST *check_warehouse(int sID)
{
    QUREY_RST *db = (QUREY_RST *)calloc(5, sizeof(int));
    char buf[128];
    MSG msg;
    QUREY_RST qr;
    msg.type = check;
    strcpy(msg.buf, currentLoginUserName);
    int ret = send(sID, &msg, sizeof(msg), 0);
    if (ret == -1)
    {
        printf("发送数据出错了\n");
        return NULL;
    }
    struct sockaddr_in serverAddr;
    socklen_t len = sizeof(serverAddr);
    // clientAddr是对方的地址,len地址的长度
    ret = recv(sID, &msg, sizeof(msg), 0);
    if (msg.type == finish)
    {
        CHECK_DATA *check_data = (CHECK_DATA *)msg.buf;
        strcpy(currentLoginHouseName, check_data->name_data); // 将仓库名称转换为全局变量
        sprintf(buf, "select * from '%s';", check_data->name_data);
        pDB = openSqlite3(check_data->name_data);
        querySql(pDB, buf, &qr);
        sprintf(buf, "select * from '%s';", qr.pzResult[qr.col * qr.row + qr.col - 1]);
        strcpy(currentLoginFormName, qr.pzResult[qr.col * qr.row + qr.col - 1]); // 将表名称转换为全局变量
        querySql(pDB, buf, &qr);
        memcpy(db, &qr, sizeof(QUREY_RST));
        return db;
    }
    return NULL;
}

// 查看云仓库数据
QUREY_RST *check_house(int sID)
{
    QUREY_RST *db = (QUREY_RST *)calloc(5, sizeof(int));
    char buf[128];
    QUREY_RST qr;
    pDB = openSqlite3("abc");
    sprintf(buf, "select * from abc where name = '%s';", currentLoginUserName);
    querySql(pDB, buf, &qr);
    strcpy(currentLoginFormName, qr.pzResult[qr.col * qr.row + qr.col - 1]);
    strcpy(currentLoginHouseName, qr.pzResult[qr.col * qr.row + qr.col - 2]);
    sprintf(buf, "select * from '%s'", qr.pzResult[qr.col * qr.row + qr.col - 1]);
    querySql(pDB, buf, &qr);
    memcpy(db, &qr, sizeof(QUREY_RST));
    return db;
}

// 打印数据表函数
void data_print(QUREY_RST *qr)
{
    printf("查询成功，为您打印数据中·········");
    for (size_t i = 0; i < ((qr->col) * (qr->row)); i++)
    {
        if (0 == i % qr->col)
        {
            printf("\n");
            printf("-----------------------------------\n");
        }
        
        printf("%s  ", qr->pzResult[i]);
    }
    printf("\n");
    return;
}

// 创建数据库函数
int initDatabase(char *pname)
{
    pDB = openSqlite3(pname);
    if (pDB == NULL)
    {
        printf("打开或者创建数据库失败\n");
        return -1;
    }
    char sql[128];
    sprintf(sql, "SELECT * FROM '%s';", pname);
    QUREY_RST qr;
    // 这里可以写一个判断，如果数据表存在了就不需要再创建
    char szSQL[256];
    sprintf(szSQL, "CREATE TABLE IF NOT EXISTS '%s' (\
           id INTEGER PRIMARY KEY AUTOINCREMENT,\
           name VARCHAR(50) NOT NULL,\
           pwd VARCHAR(50) NOT NULL,\
           warehouse_name VARCHAR(50));",
            pname);

    if (-1 == execSql(pDB, szSQL))
    {
        printf("创建用户表失败\n");
        return -1;
    }
    else
    {
        printf("已为您创建用户表\n");
    }
    return 1;
}